package com.sbj.config;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;

import com.sbj.dto.res.OrderTimeRes;
import com.sbj.dto.utils.LocalDateTimeUtils;

import com.sbj.service.IMemberService;
import lombok.AllArgsConstructor;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;


import java.time.LocalDate;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/31 18:09
 */
@Configuration
@AllArgsConstructor
public class DynamicTaskConfig {

    private final ThreadPoolTaskScheduler scheduler;

    private final IMemberService memberService;

    public void run(){
        if(!LocalDateTimeUtil.isWeekend(LocalDate.now())){
            OrderTimeRes orderTimeRes = memberService.buyTime();
            String cronExpression = LocalDateTimeUtils.convertToCronExpression(orderTimeRes.getStartTime());
            scheduler.schedule(new DynamicThread(StpUtil.getLoginIdAsInt()), new CronTrigger(cronExpression+" * * ?"));
        }
    }

}
